$(function () {
    var id = getSearch().key
    $.ajax({
        url: '/product/queryProductDetail',
        type: 'get',
        data: {
            id: id
        },
        success: function (info) {
            console.log(info)
            $('.mui-scroll').html(template('tmp', info))
        },
        complete: function () {
            mui('.mui-slider').slider({
                interval: 1000//自动轮播周期，若为0则不自动播放，默认为0；
            });
            mui('.mui-numbox').numbox()
        }
    })


    // 尺码选择
    $('.mui-scroll').on('click', '.size span', function () {
        $(this).addClass('active').siblings().removeClass('active')
    })

    // 加入购物车
    $('.btn-addCart').on('click', function () {
        var num = $('[type="number"]').val()
        var size = $('.size span.active').html()
        if (!size) {
            mui.toast('请选择尺码')
            return
        }
        $.ajax({
            url: '/cart/addCart',
            type: 'post',
            data: {
                productId: id,
                num: num,
                size: size
            },
            success: function (info) {
                console.log(info)
                if (info.success) {
                    mui.confirm('添加成功', '温馨提示', ['去购物车', '继续浏览'], function (e) {
                        if (e.index === 0) {
                            location.href = 'cart.html'
                        }
                    })
                }
                if (info.error) {
                    mui.confirm('添加失败,请先登录', '温馨提示', ['继续浏览', '去登录'], function (e) {
                        if (e.index === 1) {
                            location.href = 'login.html?backUrl=' + location.href
                        }
                    })
                }
            }
        })
    })
})